package com.nana.framework.ui.manager
{
	import com.greensock.TweenLite;
	import com.nana.framework.base.App;
	import com.nana.framework.ui.UIComponent;
	import com.nana.framework.ui.events.PopUpEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	public class PopUpManager extends EventDispatcher
	{
		private var _stage:Stage;
		private var _parent:DisplayObjectContainer;
		public var oldWindowVisible:Boolean = false;
		protected var modalSprite:Sprite;
		protected var windowModalDic:Dictionary;
		protected var windowCenterDic:Dictionary;
		protected var windowPriorityDic:Dictionary;
		private var windowList:Array;
		
		public function PopUpManager(){
			this._stage = App.appStage;
			this.modalSprite = new Sprite();
			this.modalSprite.graphics.beginFill(0, 0.5);
			this.modalSprite.graphics.drawRect(0, 0, 10, 10);
			this.modalSprite.graphics.endFill();
			this.modalSprite.width = this._stage.stageWidth;
			this.modalSprite.height = this._stage.stageHeight;
			this.windowModalDic = new Dictionary();
			this.windowCenterDic = new Dictionary();
			this.windowPriorityDic = new Dictionary();
			this.windowList = [];
			this._stage.addEventListener(Event.RESIZE, this.resizeHandler);
		}
		
		public function get parent():DisplayObjectContainer{
			return (this._parent || this._stage);
		}
		public function set parent(value:DisplayObjectContainer):void{
			this._parent = value;
		}
		private function resizeHandler(_arg1:Event):void{
			this._resizeHandler(this._stage.stageWidth, this._stage.stageHeight);
		}
		
		protected function _resizeHandler(width:Number, height:Number):void{
			var window:DisplayObject = this.getWindow();
			if (window != null){
				if (this.windowCenterDic[window] == true){
					window.x = ((width - window.width) / 2);
					window.y = ((height - window.height) / 2);
				}
			}
			this.modalSprite.width = width;
			this.modalSprite.height = height;
		}
		
		public function getWindow():DisplayObject{
			if (this.windowList.length > 0){
				return this.windowList[this.windowList.length - 1];
			}
			return null;
		}
		
		public function getWindowList():Array{
			return this.windowList;
		}
		
		public function addPopUp(window:DisplayObject, isModal:Boolean=false, isCenter:Boolean=true, priority:int=1):void{
			var _local5:int;
			var _local7:DisplayObject;
			this.windowModalDic[window] = isModal;
			this.windowCenterDic[window] = isCenter;
			this.windowPriorityDic[window] = priority;
			if (this.windowList.length > 0){
				for (var i:int = 0; i < this.windowList.length; i++) {
					var windowTemp:DisplayObject = this.windowList[i];
					// 如果优先级高于列表中的某个，加到其后
					if (this.windowPriorityDic[window] < this.windowPriorityDic[_local7]){
						this.windowList.splice(_local5, 0, window);
						break;
					}
					// 加到弹窗列表最后
					if (_local5 == (this.windowList.length - 1)){
						this.windowList.push(window);
						break;
					}
				}
			} else {
				this.windowList.push(window);
			}
			// 隐藏旧弹窗
			if (!this.oldWindowVisible){
				for (var j:int = 0; j < this.windowList.length - 1; j++) {
					(this.windowList[_local5] as DisplayObject).visible = false;
				}
			}
			if (this.windowList.length > 0 && !(this.windowList[this.windowList.length - 1] == window)){
				// ??
			} else {
				this._addPopUp(window);
			}
			var event:PopUpEvent = new PopUpEvent(PopUpEvent.POP_UP_OPEN);
			event.targetWindow = window;
			event.windowList = this.windowList;
			dispatchEvent(event);
		}
		
		public function createPopUp(popClass:Class, isModal:Boolean=false, isCenter:Boolean=true, _arg4:Object=null, priority:Number=1):UIComponent{
			var popUp:UIComponent = new popClass() as UIComponent;
			if (popUp == null){
				throw (new Error("必须是UIComponent或其子类"));
			}
			if (popUp.hasOwnProperty("data")){
				popUp["data"] = _arg4;
			}
			this.addPopUp(popUp, isModal, isCenter, priority);
			return popUp;
		}
		
		private function _addPopUp(popUp:DisplayObject):void{
			popUp.visible = true;
			if (this.windowModalDic[popUp]){
				this.parent.addChild(this.modalSprite);
			}
			this.addPopUpToStage(popUp, this.windowCenterDic[popUp]);
		}
		
		protected function addPopUpToStage(popUp:DisplayObject, animated:Boolean):void{
			this.parent.addChild(popUp);
			if (animated){
				var scale:Number = 0.5;
				var tempX:int = (this._stage.stageWidth - popUp.width) / 2;
				var tempY:int = (this._stage.stageHeight - popUp.height) / 2;
				popUp.x = (this._stage.stageWidth - (popUp.width * scale)) / 2;
				popUp.y = (this._stage.stageHeight - (popUp.height * scale)) / 2;
				popUp.scaleX = scale;
				popUp.scaleY = scale;
				popUp.alpha = 0.5;
				TweenLite.to(popUp, 0.1, {
					x:tempX,
					y:tempY,
					scaleX:1,
					scaleY:1,
					alpha:1,
					onComplete:this.popUpComplete
				});
			}
		}
		
		private function popUpComplete():void{
			var event:PopUpEvent = new PopUpEvent(PopUpEvent.POP_UP_COMPLETE);
			dispatchEvent(event);
		}
		
		public function removePopUp(popUp:DisplayObject):void{
			if (popUp && this.parent.contains(popUp)){
				this.removePopUpToStage(popUp);
			}
			if (this.parent.contains(this.modalSprite)){
				this.parent.removeChild(this.modalSprite);
			}
			delete this.windowModalDic[popUp];
			delete this.windowCenterDic[popUp];
			delete this.windowPriorityDic[popUp];
			var index:int = this.windowList.indexOf(popUp);
			if (index != -1){
				this.windowList.splice(index, 1);
			}
			if (this.windowList.length > 0 && (index == this.windowList.length)){
				this._addPopUp((this.windowList[(this.windowList.length - 1)] as DisplayObject));
			}
			var event:PopUpEvent = new PopUpEvent(PopUpEvent.POP_UP_CLOSE);
			event.targetWindow = popUp;
			event.windowList = this.windowList;
			dispatchEvent(event);
		}
		
		protected function removePopUpToStage(popUp:DisplayObject):void{
			this.parent.removeChild(popUp);
		}
	}
}